home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PRINTER / JPSRC11.ARJ / JETPRINT.C < prev    next >
C/C++ Source or Header  |  1991-08-04  |  11KB  |  375 lines

  1. /*
  2.  *      JET PAK - HP DeskJet and LaserJet series printer utilities
  3.  *
  4.  *      JETPRINT program - print a soft font file summary sheet
  5.  *
  6.  *      Version 1.1 (Public Domain)
  7.  */
  8.  
  9. /* system include files */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. /* application include files */
  15. #include "patchlev.h"
  16. #include "jetfont.h"
  17. #include "jetmean.h"
  18. #include "jetutil.h"
  19.  
  20. /*
  21.  * MODULE GLOBAL DATA
  22.  */
  23.  
  24. /* buffer of characters to send to printer */
  25. static char pb[BUFSIZ];
  26.  
  27. /* font command being processed */
  28. static FONT_COMMAND fc;
  29.  
  30. /* optimum line spacing */
  31. static UNSIGNEDINT font_lpi;
  32.  
  33. /* choose a lucky number to use for the temporary font */
  34. #define FONTNUMBER 11459
  35.  
  36. /* these control the layout of the sheet; the idea is that it should
  37.    work regardless of the printer's default font: */
  38. #define MARGIN   200        /* margin used for all lines */
  39. #define RHS      1000       /* position of right hand column in table */
  40. #define DELTA    125        /* offset between chart columns */
  41. #define CPR      16         /* characters per row */
  42.  
  43. /* line beginning and ending macros */
  44. #define _LB sprintf(pb, "\033*p%dX", MARGIN); os_printstr(pb);
  45. #define _LE sprintf(pb, "\r\n"); os_printstr(pb);
  46.  
  47. /*
  48.  * LOCAL FUNCTIONS
  49.  */
  50.  
  51. static void usage_wrong()
  52. {
  53.     /*
  54.      * Print usage message and exit.
  55.      */
  56.  
  57.     fprintf(stderr, USAGE_HEADER);
  58.     fprintf(stderr, "Usage: JETPRINT [-h] fontfile [fontfile...]\n\n");
  59.     fprintf(stderr, "Print soft font file summary sheets\n\n");
  60.     fprintf(stderr, "  -h            print this usage information\n");
  61.     fprintf(stderr, "  fontfile      soft font file name\n\n");
  62.     exit(1);
  63. }
  64.  
  65. static char *selection_sequence()
  66. {
  67.     static char retstr[100];
  68.  
  69.     if (fc.data.font.spacing == PROPORTIONAL)
  70.     {
  71.         sprintf(retstr, "Ec&l%dOEc(%d%cEc(s%du%dp%.1fv%ds%db%dt%dQ",
  72.             fc.data.font.orientation,
  73.             fc.data.font.symbol_set/32,
  74.             '@'+fc.data.font.symbol_set%32,
  75.             fc.data.font.placement,
  76.             fc.data.font.spacing,
  77.             quarter_dots_to_points(fc.data.font.height),
  78.             fc.data.font.style,
  79.             fc.data.font.stroke_weight,
  80.             fc.data.font.typeface,
  81.             fc.data.font.quality);
  82.     }
  83.     else
  84.     {
  85.         sprintf(retstr, "Ec&l%dOEc(%d%cEc(s%du%dp%.1fh%.1fv%ds%db%dt%dQ",
  86.             fc.data.font.orientation,
  87.             fc.data.font.symbol_set/32,
  88.             '@'+fc.data.font.symbol_set%32,
  89.             fc.data.font.placement,
  90.             fc.data.font.spacing,
  91.             quarter_dots_to_lpi(fc.data.font.pitch),
  92.             quarter_dots_to_points(fc.data.font.height),
  93.             fc.data.font.style,
  94.             fc.data.font.stroke_weight,
  95.             fc.data.font.typeface,
  96.             fc.data.font.quality);
  97.     }
  98.  
  99.     return(retstr);
  100. }
  101. static void headerprint(filename)
  102. char *filename;
  103. {
  104.     /*
  105.      * Print typographical information about the font
  106.      */
  107.     UNSIGNEDINT line_spacing;
  108.  
  109.     /* the optimum line spacing in the header may be corrupt: only use
  110.        it if it lies within a reasonable range of the rule of thumb
  111.        value (1.2 times the point size) */
  112.     line_spacing = (fc.data.font.height*6)/5;
  113.     if (    (fc.data.font.text_height > line_spacing/2)
  114.          && (fc.data.font.text_height < line_spacing*2) )
  115.         line_spacing = fc.data.font.text_height;
  116.  
  117.     font_lpi = (UNSIGNEDINT)quarter_dots_to_lpi(line_spacing);
  118.  
  119.     /* switch to correct page orientation for font first */
  120.     sprintf(pb, "\033&l%dO", fc.data.font.orientation); os_printstr(pb);
  121.  
  122.     /* print the header information */
  123.          _LB; sprintf(pb, "%s\033*p%dX%s", "File Name:",           RHS, filename); os_printstr(pb);
  124.     _LE; _LB; sprintf(pb, "%s\033*p%dX%s", "Orientation:",         RHS, orientation_meaning(fc.data.font.orientation)); os_printstr(pb);
  125.     _LE; _LB; sprintf(pb, "%s\033*p%dX%s", "Character Set:",       RHS, symbol_set_meaning(fc.data.font.symbol_set)); os_printstr(pb);
  126.     _LE; _LB; sprintf(pb, "%s\033*p%dX%s", "Spacing:",             RHS, spacing_meaning(fc.data.font.spacing)); os_printstr(pb);
  127.     if (fc.data.font.spacing == FIXED)
  128.     {
  129.     _LE; _LB; sprintf(pb, "%s\033*p%dX%.1f Characters per inch", "Pitch:", RHS, quarter_dots_to_lpi(fc.data.font.pitch)); os_printstr(pb);
  130.     }
  131.     _LE; _LB; sprintf(pb, "%s\033*p%dX%.1f Points", "Point Size:", RHS, quarter_dots_to_points(fc.data.font.height)); os_printstr(pb);
  132.     _LE; _LB; sprintf(pb, "%s\033*p%dX%s", "Typestyle:",           RHS, style_meaning(fc.data.font.style)); os_printstr(pb);
  133.     _LE; _LB; sprintf(pb, "%s\033*p%dX%s", "Stroke Weight:",       RHS, stroke_weight_meaning(fc.data.font.stroke_weight)); os_printstr(pb);
  134.     _LE; _LB; sprintf(pb, "%s\033*p%dX%s", "Selection Sequence:",  RHS, selection_sequence()); os_printstr(pb);
  135.     _LE;
  136. }
  137.  
  138. static int rowprinted(font_type, row)
  139. UNSIGNEDBYTE font_type; /* HP font type */
  140. int row;                /* row number */
  141. {
  142.     /*
  143.      * Returns whether a (16 character) row in the character chart
  144.      * should be printed at all
  145.      */
  146.  
  147.     switch (font_type)
  148.     {
  149.     case FONT_TYPE_7BIT:     /* 7-bit font */
  150.         if (row >= 2 && row <= 7)
  151.             return(TRUE);
  152.         break;
  153.     case FONT_TYPE_8BIT:     /* 8-bit font */
  154.         if (row >= 2 && row <= 7)
  155.             return(TRUE);
  156.         if (row >= 10 && row <= 15)
  157.             return(TRUE);
  158.         break;
  159.     case FONT_TYPE_IBM:      /* PC-8 */
  160.         return(TRUE);
  161.     }
  162.  
  163.     return(FALSE);
  164. }
  165.  
  166. static int charprinted(font_type, code)
  167. UNSIGNEDBYTE font_type;
  168. int code;
  169. {
  170.     /*
  171.      * Returns whether a character in the chart should be printed
  172.      */
  173.     switch (font_type)
  174.     {
  175.     case FONT_TYPE_7BIT:
  176.         if (code >= 33 && code <= 127)
  177.             return(TRUE);
  178.         break;
  179.     case FONT_TYPE_8BIT:
  180.         if (code >= 33 && code <= 127)
  181.             return(TRUE);
  182.         if (code >= 160 && code <= 255)
  183.             return(TRUE);
  184.         break;
  185.     case FONT_TYPE_IBM:
  186.         if (code != 0x1b && (code < 0x07 || code > 0x0f))
  187.             return(TRUE);
  188.     }
  189.  
  190.     return(FALSE);
  191. }
  192.  
  193. static void chartprint(font_type)
  194. UNSIGNEDBYTE font_type;
  195. {
  196.     /*
  197.      * Print a chart of the whole character set
  198.      */
  199.     int i, j;
  200.  
  201.     if (font_lpi < 6)
  202.     {
  203.         /* large font - set wider line spacing */
  204.         sprintf(pb, "\033&l%dD", font_lpi);
  205.         os_printstr(pb);
  206.     }
  207.  
  208.     /* print column headers */
  209.     _LE;
  210.     for (i = 0; i < CPR; i++)
  211.     {
  212.         sprintf(pb, "\033*p%dX%02x", MARGIN + DELTA*(i+1), i);
  213.         os_printstr(pb);
  214.     }
  215.     _LE;
  216.  
  217.     /* print rows */
  218.     for (j = 0; j < 256; j += CPR)
  219.     {
  220.         if (rowprinted(font_type, j/CPR))
  221.         {
  222.             /* print row header */
  223.             sprintf(pb, "\033*p%dX%02x", MARGIN, j);
  224.             os_printstr(pb);
  225.  
  226.             /* change from default font to the font being sampled */
  227.             sprintf(pb, "\016");
  228.             os_printstr(pb);
  229.  
  230.             for (i = 0; i < CPR; i++)
  231.             {
  232.                 if (charprinted(font_type, j+i))
  233.                 {
  234.                     sprintf(pb, "\033*p%dX%c", MARGIN + DELTA*(i+1), j+i);
  235.                     os_printstr(pb);
  236.                 }
  237.             }
  238.  
  239.             /* change back to default font */
  240.             sprintf(pb, "\017");
  241.             os_printstr(pb);
  242.  
  243.             /* move to next line */
  244.             _LE;
  245.         }
  246.     }
  247.  
  248.     _LE;
  249. }
  250.  
  251. static char *poem[] = {
  252.     "In Xanadu did Kubla Khan",
  253.     "A stately pleasure-dome decree:",
  254.     "Where Alph, the sacred river, ran",
  255.     "Through caverns measureless to man",
  256.     "Down to a sunless sea.",
  257.     "So twice five miles of fertile ground",
  258.     "With walls and towers were girdled round.",
  259. };
  260.  
  261. static void textprint()
  262. {
  263.     int i;
  264.  
  265.     /* set optimum line spacing for font */
  266.     sprintf(pb, "\033&l%dD", font_lpi);
  267.     os_printstr(pb);
  268.  
  269.     /* change to the downloaded font */
  270.     sprintf(pb, "\016");
  271.     os_printstr(pb);
  272.  
  273.     for (i = 0; i < sizeofarray(poem); i++)
  274.     {
  275.         _LB; os_printstr(poem[i]); _LE;
  276.     }
  277. }
  278.  
  279. static void jetprint()
  280. {
  281.     char inpath[OS_PATH_LEN];
  282.     FILE *infp;
  283.     int r;
  284.     UNSIGNEDBYTE font_type;
  285.  
  286.     /* build the input file path */
  287.     strcpy(inpath, os_dir);
  288.     strcat(inpath, os_file);
  289.  
  290.     if (!(infp = fopen(inpath, "rb")))
  291.     {
  292.         fprintf(stderr, ERROR_OPEN_READ_FAILED, os_dir, os_file);
  293.         return;
  294.     }
  295.  
  296.     /* just read the header information */
  297.     while (    ((r = font_command_read(infp, &fc)) == OK)
  298.             && (fc.command_type != FDC) )
  299.         ;
  300.  
  301.     if (r != OK)
  302.     {
  303.         /* font header not found - probably not a soft font file */
  304.         fprintf(stderr, ERROR_FILE_READ_FAILED, os_dir, os_file);
  305.         fclose(infp);
  306.         return;
  307.     }
  308.  
  309.     /* reset printer, set CR and LF to be interpreted as is */
  310.     sprintf(pb, "\033E\033&k0G");
  311.     os_printstr(pb);
  312.  
  313.     /* download the font as temporary and secondary */
  314.     sprintf(pb, "\033*c%dD\033*c4F", FONTNUMBER);
  315.     os_printstr(pb);
  316.  
  317.     rewind(infp);
  318.     while ((r = fread(pb, 1, BUFSIZ, infp)) > 0)
  319.         os_printbuf(pb, r);
  320.  
  321.     /* print information from header */
  322.     headerprint(inpath);
  323.     font_type = fc.data.font.type;
  324.  
  325.     /* select the downloaded font as the secondary font */
  326.     sprintf(pb, "\033)%dX", FONTNUMBER);
  327.     os_printstr(pb);
  328.  
  329.     /* print the character set chart */
  330.     chartprint(font_type);
  331.  
  332.     /* print the sample text */
  333.     textprint();
  334.  
  335.     /* reset printer */
  336.     sprintf(pb, "\033E");
  337.     os_printstr(pb);
  338.     fclose(infp);
  339.  
  340.     fprintf(stderr, OK_JETPRINT, os_dir, os_file);
  341. }
  342.  
  343. main(argc, argv)
  344. int argc;
  345. char *argv[];
  346. {
  347.     char c;
  348.  
  349.     /* stop getopt() printing errors */
  350.     opterr = FALSE;
  351.     while ((c = getopt(argc, argv, "h")) != EOF)
  352.     {
  353.         switch (c)
  354.         {
  355.         case 'h':
  356.         case '?':
  357.             /* help required, or invalid option specified */
  358.             usage_wrong();
  359.         }
  360.     }
  361.  
  362.     /* must specify at least one file */
  363.     if (optind >= argc)
  364.         usage_wrong();
  365.  
  366.     /* process file arguments */
  367.     if (os_findfiles((argc - optind), &argv[optind]) == ERROR)
  368.         fprintf(stderr, ERROR_OUT_OF_HEAP);
  369.  
  370.     while (os_getfile() != EOF)
  371.         jetprint();
  372.  
  373.     return(0);
  374. }
  375.